<?php
namespace Kuoqi;

use Kuoqi;
/**
 * Class Url
 * @package Kuoqi
 * User kuoqi
 * Datetime 2022/2/26 23:07
 * Remark 核心组键 - 关于url这块的基础对象，功能的话，看发挥吧，应该功能挺多的
 */
class Url{

    /**
     * @param $stream
     * @return array
     * User kuoqi
     * Datetime 2022/2/26 23:34
     * Remark url query to array 将字符串信息转换为数组的类型
     */
    static public function query2array($stream){
        $tmps = explode('&',$stream);
        $arr = array();
        foreach ($tmps as $tmp){
            $temp = explode('=',$tmp);
            if(count($temp)==2){
                $arr[$temp[0]] = urldecode($temp[1]);
            }elseif(count($temp)==1)
            {
                $arr[$temp[0]] = null;
            }
        }
        return $arr;
    }

    /**
     * @param $arr
     * @return string
     * User kuoqi
     * Datetime 2022/2/27 9:24
     * Remark 数组转query字符串
     */
    static public function array2query($arr){
        if(function_exists('http_build_query')){
            return http_build_query($arr);
        }else{
            $tmp = array();
            foreach ($arr as $k=>$v){
                $tmp[] = sprintf('%s=%s',$k,$v);
            }
            return implode('&',$tmp);
        }
    }

    /**
     * @param $arr
     * @return string
     * User kuoqi
     * Datetime 2022/2/27 9:27
     * Remark 数组转query string的路由信息
     */
    static public function array2route($arr){
        $tmp = array();
        foreach ($arr as $k=>$v){
            $tmp[] = sprintf('%s/%s',$k,$v);
        }
        return $tmp?('/'.implode(Params::$RL,$tmp)):'';
    }



    /**
     * @var string
     * User kuoqi
     * Datetime 2022/2/26 23:12
     * Remark 输入的链接信息的备份，一般使用场景为获取系统输入路径等基本信息
     */
    protected $input_link = '';

    /**
     * @var string
     * User kuoqi
     * Datetime 2022/2/26 23:22
     * Remark url的path部分，以斜杠开头是免不了的
     */
    protected $path = '';
    /**
     * @var array
     * User kuoqi
     * Datetime 2022/2/26 23:22
     * Remark url的query信息
     */
    protected $query = array();
    /**
     * @var string
     * User kuoqi
     * Datetime 2022/2/26 23:21
     * Remark www.kuoqs.com..
     */
    protected $host = '';
    /**
     * @var string
     * User kuoqi
     * Datetime 2022/2/26 23:21
     * Remark url scheme->http or https
     */
    protected $scheme = 'http';
    /**
     * @var string
     * User kuoqi
     * Datetime 2022/2/26 23:21
     * Remark url->#
     */
    protected $fragment = '';

    /**
     * @var string
     * User kuoqi
     * Datetime 2022/3/1 12:55
     * Remark 除了参数部分之外的路由信息
     */
    protected $route = '';

    /**
     * Url constructor.
     * @param string $route
     * @param array $extParams
     * @param string $host
     * @param string $schem
     * 初始化
     */
    public function __construct($path = '',$query = array(),$host = '',$scheme = '',$fragment = '')
    {
        $path && ($this->path=$path);
        $query && ($this->query=$query);
        $host && ($this->host=$host);
        if(!$this->host){
            $this->host = isset($_SERVER['HTTP_HOST'])?$_SERVER['HTTP_HOST']:'localhost:80';
        }
        if($scheme=='https'){
            $this->scheme='https';
        }elseif(isset($_SERVER['HTTPS'])&&$_SERVER['HTTPS']=='on'){
            $this->scheme = 'https';
        }else{
            $this->scheme = 'http';
        }
        $fragment && ($this->fragment=$fragment);
    }



    /**
     * 获取系统输入的url链接信息
     * @param string $link
     * @return string
     */
    public function getInput($link = ''){
        if($link){
            $this->input_link = $link;
        }else{
            $this->input_link = $_SERVER['REQUEST_URI'];
        }
         $arr = parse_url($this->input_link);
         if(isset($arr['scheme'])){ $this->scheme = $arr['scheme'];}
         if(isset($arr['host'])){ $this->host = $arr['host'];}
         if(isset($arr['path'])){ $this->path = $arr['path'];}
         if(isset($arr['query'])){ $this->query = self::query2array($arr['query']);}
         if(isset($arr['fragment'])){ $this->fragment = $arr['fragment'];}
         return $this->input_link;
    }

    /**
     * 解析当前基础链接进入完整的信息,尝试将当前链接的所有信息转换为路由相关的信息，这个是非常非常复杂和懒得，这里做了之后就可以直接将这个当做路由来使用了
     * User kuoqi
     * Datetime 2022/2/26 23:16
     * Remark
     */
    public function exec2route(){
        $path = $this->path;
        if(Params::$INDEX_PHPINFO==1){
            $path = str_replace(Params::$INDEX,'',$path);
        }
        //去除伪静态信息
        $path = preg_replace(Params::$INDEX_FEIGNZZ,'/',$path);
        $route = new Route($path,$this->query);
        $route->exec();//解析路由信息
        return $route;
    }

    /**
     * @param $n 参数名，可以为数组或者字符串
     * @param null $v 补充参数，若$m为字符串的时候本参数为其补充值
     * User kuoqi
     * Datetime 2022/3/1 19:55
     * Remark 增加链接属性信息
     */
    public function addQuery($n,$v = null){
        if(is_array($n)){
            foreach($n as $k=>$vv){
                $this->addQuery($k,$vv);
            }
        }else{
            $this->query[$n] = $v;
        }
    }


    /**
     * 解析为url链接信息
     * @param bool $isComplex 是否完整路径，true为携带域名等完整的链接，false为常规链接
     * @return mixed|string
     * @throws Error
     */
    public function execTolink($isComplex = false){
        $route = $this->exec2route();//获取到路由对象信息
        $link = str_replace(Params::$RL,Params::$LL,$route->flushRoot());
        if(Params::$INDEX_PHPINFO==1){
            $link = Params::$INDEX.$link;
        }
        $params = array();
        foreach($route->params as $k=>$v){
            if(in_array($k,array('m','c','a'))){
                //$params[$k] = Route::code2hump($v);
                continue;
            }
            $params[$k] = $v;
        }
        $params = http_build_query($params);
        if($params){
            $link .= '?'.$params;
        }
        if($isComplex){
            $link = $this->scheme.'://'.$this->host.$link;//获取完整版地址
        }
        return $link;
    }

    /**
     * @param null $link
     * @param array $params
     * User kuoqi
     * Datetime 2022/2/28 23:30
     * Remark 根据反向路由来配置url相关参数信息
     */
    public function setUrlbyRoute($link = null,$params = array()){
        $this->path = $link;
        $this->query = $params;
    }

    /**
     * 获取服务头的部分
     * @return string
     */
    public function getHead(){
        return $this->scheme.'//'.$this->host;
    }
    /**
     * 简单的url链接组装
     * @param bool $isComplex
     * @return string
     */
    public function toUrl($isComplex = false){
        $link = $this->path;
        if($isComplex){
            $link = $this->scheme.'://'.$this->host.$link;//获取完整版地址
        }
        return $link;
    }


}